const userservice = require('../services/userservice')
const express = require('express')
const router = express.Router()

const multer = require('multer');
const upload = multer();

//注册方法
router.get('/register', function (req, res) {
    var name = req.query.name;
    var pass = req.query.pass;
    var mail = req.query.mail;
    //输入异常检测
    if (name == null || pass == null || mail == null) {
        res.send(JSON.stringify({
            success: 0,
            log: 'wrong input'
        }))
        return;
    }
    //service注册方法执行
    userservice.register(mail, name, pass, function (success, log) {
        res.send(JSON.stringify({
            success: success,
            log: log
        }))
    })
})

router.all('/login', upload.none(), function (req, res) {
    let gettingtype
    if (req.method.toLowerCase() == 'post')
        gettingtype = req.body
    else
        gettingtype = req.query
    var mail = gettingtype.mail
    var pass = gettingtype.pass
    if (mail == null || pass == null) {
        res.send(JSON.stringify({
            success: 0,
            log: 'wrong input'
        }))
        return;
    }
    userservice.login(mail, pass, function (data) {
        res.send(JSON.stringify(data))
    })
})

module.exports = router